local kenjie = fk.CreateSkill{
  name = "wk_heg__kenjie",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["wk_heg__kenjie"] = "恳节",
  [":wk_heg__kenjie"] = "锁定技，当你成为延时锦囊牌的目标时，取消之；其他角色获得你的牌后，其弃置之。",

  ["$wk_heg__kenjie1"] = "秽尘天听，卿有不测之祸！",
  ["$wk_heg__kenjie2"] = "倾乱国政，安得寿终正寝？",
}

kenjie:addEffect(fk.AfterCardsMove, {
  anim_type = "defensive",
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(kenjie.name) then return false end
    local cardInfo = {}
    local cardTo
    for _, move in ipairs(data) do
      if move.to and move.to ~= player and move.toArea == Card.PlayerHand 
        and move.from and move.from == player then
        for _, info in ipairs(move.moveInfo) do
          table.insert(cardInfo, info.cardId)
        end
        cardTo = move.to
      end
    end
    if #cardInfo > 0 then
      event:setCostData(self, { cards = cardInfo, cardto = cardTo})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    if #event:getCostData(self).cards > 0 then
      local to = event:getCostData(self).cardto
      player.room:throwCard(event:getCostData(self).cards, kenjie.name, to, to)
    end
  end,
})

kenjie:addEffect(fk.BeforeCardsMove, {
  anim_type = "defensive",
  mute = true,
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(kenjie.name) then return false end
    local id = 0
    local source = player
    local c
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Card.PlayerJudge then
        for _, info in ipairs(move.moveInfo) do
          id = info.cardId
          if info.fromArea == Card.PlayerJudge then
            source = move.from or player
          else
            source = player
          end
          c = source:getVirualEquip(id)
          --FIXME：巨大隐患，延时锦囊的virtual_equips在置入判定区的事件被篡改，或者判定阶段自然流程以外的方式离开判定区时不会清理
          if not c then c = Fk:getCardById(id) end
          if c.trueName == "indulgence" or c.trueName == "supply_shortage" or c.trueName == "lightning" then
            return true
          end
        end
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, kenjie.name)
    player:broadcastSkillInvoke(kenjie.name, 1)
    local source = player
    local mirror_moves = {}
    local ids = {}
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Card.PlayerJudge then
        local move_info = {}
        local mirror_info = {}
        for _, info in ipairs(move.moveInfo) do
          local id = info.cardId
          if info.fromArea == Card.PlayerJudge then
            source = move.from or player
          else
            source = player
          end
          local c = source:getVirualEquip(id)
          if not c then c = Fk:getCardById(id) end
          if c.trueName == "indulgence" or c.trueName == "supply_shortage" or c.trueName == "lightning" then
            table.insert(mirror_info, info)
            table.insert(ids, id)
          else
            table.insert(move_info, info)
          end
        end
        if #mirror_info > 0 then
          move.moveInfo = move_info
          local mirror_move = move:copy()
          mirror_move.to = nil
          mirror_move.toArea = Card.DiscardPile
          mirror_move.moveInfo = mirror_info
          table.insert(mirror_moves, mirror_move)
        end
      end
    end
    table.insertTable(data, mirror_moves)
  end
})


return kenjie